package com.producer.死信队列;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class DeadQueueController {
	@RequestMapping(value="/sendDeadQueueMessage.do",method=RequestMethod.GET)
	@ResponseBody
	public String sendMessage(@RequestParam String message) {
		Map<String, String> msg = new HashMap<String, String>();
		msg.put("message", message);

		MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
			@Override
			public Message postProcessMessage(Message message) throws AmqpException {
				//单独设置消息的过期时间
				message.getMessageProperties().setExpiration("15000");
				return message;
			}
		};

		rabbitTemplate.convertAndSend(DeadQueueConfig.MAIN_EXCHANGE_NAME, DeadQueueConfig.MAIN_QUEUE_ROUTINGKEY, msg,messagePostProcessor);
		return "消息发送成功";
	}
	private final RabbitTemplate rabbitTemplate;

	@Autowired
	public DeadQueueController(RabbitTemplate rabbitTemplate){
		this.rabbitTemplate = rabbitTemplate;
	}
}
